﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using My3DGame.Game_Base.Cameras;


namespace My3DGame
{
    public class clsMenuState : clsStateGame 
    {
        clsMenuManager menuManager;
        KeyboardState oldKeyboardState;
        List<Texture2D> menuBackground;

        // Model stuff
        clsMenuModelManager clsMenuModelManager;

        // Camera
        public clsCameraBackground camera { get; protected set; }

        // Random
        public Random rand { get; protected set; }

        // Audio
        AudioEngine audioEngine;
        WaveBank waveBank;
        SoundBank soundBank;
        Cue trackCue;


        public clsMenuState(Game1 game1)
        {
            nextGameState = clsStateGameManager.StateGame.EXIT;
            rand = new Random();
        }

        public override void Initialize(Game1 game1)
        {
            menuManager = new clsMenuManager(@"Game States/Menus/Menu.xml");
            clsResourceHelper.LoadMenuImages();
            menuBackground = clsResourceHelper.MenuImages;

            // Initialize Camera
            camera = new clsCameraBackground(this, new Vector3(0, 0, 50), Vector3.Zero, Vector3.Up);
            Components.Add(camera);

            // Initialize model manager
            clsMenuModelManager = new clsMenuModelManager(this, camera, game1);
 
            #region Background Menu


            #endregion
        }

        public override void LoadContent(Game1 game1)
        {
            oldKeyboardState = Keyboard.GetState();

            // Load sounds and play initial sounds
            audioEngine = new AudioEngine(@"Content\Audio\GameAudio.xgs");
            waveBank = new WaveBank(audioEngine, @"Content\Audio\Wave Bank.xwb");
            soundBank = new SoundBank(audioEngine, @"Content\Audio\Sound Bank.xsb");
            
            // Play the soundtrack
            trackCue = soundBank.GetCue("Tracks");
            trackCue.Play();

            base.LoadContent();
        }

        public override void Update(GameTime gameTime, Game1 game1)
        {
            #region Xử lý menu với bàn phím.

            KeyboardState keyboard = Keyboard.GetState();
            if (oldKeyboardState != keyboard)
            {
                if (keyboard.IsKeyDown(Keys.Escape))
                {
                    menuManager.BackToParentsMenu();
                }
                if (keyboard.IsKeyDown(Keys.Enter))
                {
                    if (menuManager.CurrentMenu.SubItems[menuManager.CurrentMenu.ChoiceIndex].Name == "Start Game")
                    {
                        nextGameState = clsStateGameManager.StateGame.START;
                        game1.stateGameManager.NextState(game1);
                    }
                    else if (menuManager.CurrentMenu.SubItems[menuManager.CurrentMenu.ChoiceIndex].Name == "Quit")
                    {
                        nextGameState = clsStateGameManager.StateGame.EXIT;
                        game1.stateGameManager.NextState(game1);
                    }
                    else if (menuManager.CurrentMenu.SubItems[menuManager.CurrentMenu.ChoiceIndex].Name == "Back")
                    {
                        menuManager.BackToParentsMenu();
                    }
                    else if (menuManager.CurrentMenu.SubItems[menuManager.CurrentMenu.ChoiceIndex].Name == "About")
                    {
                        nextGameState = clsStateGameManager.StateGame.ABOUT;
                        game1.stateGameManager.NextState(game1);
                    }
                    else if(menuManager.CurrentMenu.SubItems[menuManager.CurrentMenu.ChoiceIndex].Name == "Help")
                    {
                        nextGameState = clsStateGameManager.StateGame.HELP;
                        game1.stateGameManager.NextState(game1);
                    }
                    else if (menuManager.CurrentMenu.SubItems[menuManager.CurrentMenu.ChoiceIndex].Name == "On")
                    {
                        // Âm thanh bật
                        game1.onAudio = true;
                    }
                    else if (menuManager.CurrentMenu.SubItems[menuManager.CurrentMenu.ChoiceIndex].Name == "Off")
                    {
                        // Âm thanh tắt
                        game1.onAudio = false;
                    }
                    else if (menuManager.CurrentMenu.SubItems[menuManager.CurrentMenu.ChoiceIndex].Name == "Full Screen")
                    {
                        game1.graphics.IsFullScreen = true;
                        game1.graphics.ApplyChanges();
                    }
                    else if (menuManager.CurrentMenu.SubItems[menuManager.CurrentMenu.ChoiceIndex].Name == "Window")
                    {
                        game1.graphics.IsFullScreen = false;
                        game1.graphics.ApplyChanges();
                    }
                    else
                    {
                        menuManager.EnterSubMenu(menuManager.CurrentMenu.ChoiceIndex);
                    }
                }
                if (keyboard.IsKeyDown(Keys.Down))
                {
                    menuManager.ChosseLowerObject();
                }

                if (keyboard.IsKeyDown(Keys.Up))
                {
                    menuManager.ChooseUpperObject();
                }
            }
            oldKeyboardState = keyboard;

            #endregion

            #region Xử lý menu với gamePad

            //  Đang xây dựng. Đuối rồi.

            #endregion

            if(game1.onAudio == false)
            {
                trackCue.Pause();
            }
            else
            {
                trackCue.Resume();
            }
            clsMenuModelManager.Update(gameTime);
        }

        public override void Draw(GameTime gameTime, Game1 game)
        {
            game.spriteBatch.Begin();
            //game.GraphicsDevice.Clear(Color.Black);
            ShowMenu(menuManager, game);
            clsMenuModelManager.Draw(gameTime);

            game.spriteBatch.End();
        }

        public void ShowMenu(clsMenuManager menuMgr, Game1 game)
        {
            // Load font for menu Items.
            SpriteFont spriteFont = game.Content.Load<SpriteFont>(@"Fonts/Pericles");
            Vector2 size = new Vector2();

            // Vẽ menu.
            try
            {
                float X, Y;
                X = spriteFont.MeasureString(menuMgr.CurrentMenu.Name).X;
                Y = 0;
                for (int i = 0; i < menuMgr.CurrentMenu.SubItems.Count; i++)
                {
                    // Tính độ lớn của chuỗi khi xuất ra màn hình
                    size = spriteFont.MeasureString(menuMgr.CurrentMenu.SubItems[i].Name);
                    if (size.X > X)
                        X = size.X;
                    if (size.Y > Y)
                        Y = size.Y;
                }

                Point SizeBox = new Point((int)X / 40 + 5,
                    (int)(Y * menuMgr.CurrentMenu.SubItems.Count) / 40 + 6);
                Vector2 pos = new Vector2(
                    game.Window.ClientBounds.Width / 2 - SizeBox.X * 20,
                    game.Window.ClientBounds.Height / 2 - SizeBox.Y * 40 / 2 - 50);

                game.spriteBatch.Draw(menuBackground[0], new Vector2(game.Window.ClientBounds.Width / 2 - size.X / 2 - 10,
                    pos.Y + 70 - size.Y / 2),
                Color.White);

                size = spriteFont.MeasureString(menuMgr.CurrentMenu.Name);
                game.spriteBatch.DrawString(spriteFont, menuMgr.CurrentMenu.Name,
                    new Vector2(game.Window.ClientBounds.Width / 2 - size.X / 2,
                    pos.Y + 70 - size.Y / 2),
                Color.GreenYellow);
                Vector2 origin = new Vector2(0, spriteFont.LineSpacing / 2);
                for (int i = 0; i < menuMgr.CurrentMenu.SubItems.Count; i++)
                {
                    String submenu = menuMgr.CurrentMenu.SubItems[i].Name;
                    size = spriteFont.MeasureString(submenu);
                    //float pulsate = (float)Math.Sin(123 * 6) + 1;

                    //float scale = 1 + pulsate * 0.05f * selectionFade;

                    if (i == menuMgr.CurrentMenu.ChoiceIndex)
                        game.spriteBatch.DrawString(
                            spriteFont,
                            submenu,
                            new Vector2(game.Window.ClientBounds.Width / 2 - size.X / 2,
                                pos.Y + 140 + size.Y * i),
                            Color.Gold,
                            0,
                            new Vector2(0,0), 
                            1.2f,
                            SpriteEffects.None, 0);
                    else
                        game.spriteBatch.DrawString(
                            spriteFont,
                            submenu,
                            new Vector2(game.Window.ClientBounds.Width / 2 - size.X / 2,
                                pos.Y + 140 + size.Y * i),
                            Color.White);
                }
            }
            catch (Exception e)
            {
                clsErrorMsgHelper.WriteError("Cannot Load Menu Items/nDetail:/n" + e.Message);
            }
        }
     
    }
}
